package mk.edu.ii.navigatemacedonia;

import java.util.List;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView.LayoutParams;  
import com.google.android.maps.Overlay;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.view.MotionEvent;
import android.graphics.Canvas;
import android.graphics.Paint;


/**
 * This class is used for show map from Google Maps.
 * There are 2 working mods:navigation and selection.
 * Map have 3 view mods:satelite, street view and trafic view. 
 * @author  G.K. N.A. M.A.
 */
public class PickLocationMapView extends MapActivity {
	private MapView mapView;
	
	private boolean locationSelectionMode;
	private int[] regionModeText = {100,40}; //x,y
	
	/** Called when the activity is first created. 
	 * 
	 */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.mappreviewactivity);
		mapView = (MapView)findViewById(R.id.map_view);
	    locationSelectionMode = false;
		
		mapView.setStreetView(false);
		mapView.setSatellite(true); 
		mapView.setTraffic(false);
		//GeoPoint point=new GeoPoint(42,21);
		//mapView.getController().setCenter(point);
		//mapView.getController().setZoom(4);
		
        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
        View zoomView = mapView.getZoomControls(); 
 
        zoomLayout.addView(zoomView, 
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);
      
   
        
        MapOverlay mapOverlay = new MapOverlay();
        
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        
        mapView.invalidate();        
	}
	
	/** Called when the Menu is created.
	 * 
	 */
	@Override
    public boolean onCreateOptionsMenu(Menu menu){
    	super.onCreateOptionsMenu(menu);
    	
    	menu.add(0, 0, 0, "Street View");
    	menu.add(0,1,1,"Satellite");
    	menu.add(0,2,2,"Traffic");
    	return true;
    } 
	/** Called when we select item from Menu.
	 * 
	 */
	public boolean onOptionsItemSelected(MenuItem item){
    	super.onOptionsItemSelected(item);
    	switch (item.getItemId()) {
       	case 0:{
       		mapView.setStreetView(true);
       		mapView.setSatellite(false);
       		mapView.setTraffic(false);
       		return true;
       	}
       	case 1:{

       		mapView.setStreetView(false);
       		mapView.setSatellite(true);
       		mapView.setTraffic(false);
       		return true;
       	}
       	case 2:{
       		mapView.setStreetView(false);
       		mapView.setSatellite(false);
       		mapView.setTraffic(true);
       		return true;
       	}
       	
    	}
    	return true;
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// IMPORTANT: This method must return true if your Activity
		// is displaying driving directions. Otherwise return false.
		return false;
	}

	public PickLocationMapView  pointer()
	{
		return this;
	}
	
	/** Class for overlay on the map, to showing navigation
	 * and selection mod
	 * @author G.K. N.A. M.A. */
	class MapOverlay extends com.google.android.maps.Overlay
	{
		boolean selection = false;
		/** Called when we touch the screen.
		 * 
		 */
	    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
	    {   
	    	Log.i("TOUCH_EVENT: event", String.valueOf(event.getAction()));
	    	//---when user lifts his finger---
	    		    	
	        if (selection && event.getAction()==1) {                
	            GeoPoint p = mapView.getProjection().fromPixels(
	                (int) event.getX(),
	                (int) event.getY());
	         
	                Double pom1 = p.getLatitudeE6() / 1E6 ;
	                Double pom2 = p.getLongitudeE6() / 1E6 ;
	               
	                //vrati se vo prethodniot prozor so podatoci prevzemeni od mapata
	                Intent autocomplete = new Intent(pointer(),NavigateMacedonia.class);
	                autocomplete.putExtra("latitude", pom1);
	                autocomplete.putExtra("longitude", pom2);
	                //za lokacijata
	                startActivity(autocomplete);
	                 }
	        if(event.getAction()==0){
	        	selection=true;
	        }
	        if(event.getAction()==2){
	        	selection=false;
	        }
	    	
	        return false;
	    }
	    
	}
	
	
}

